﻿using Microsoft.Graphics.Canvas;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Graphics.Imaging;
using Windows.Storage;
using Windows.Storage.Streams;

namespace 纸片人.win2dEffectControl.DataClass
{
    public abstract class EffectData : IEffectData,INotifyPropertyChanged
    {
        #region 属性更改通知接口实现
        public event PropertyChangedEventHandler PropertyChanged;
         
        protected void OnPropertyChanged([CallerMemberName] string name = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
        #endregion


        public EffectData()
        {
            AddOutputSequene(this,false);
        }

        

        /// <summary>
        /// 输出队列
        /// </summary>
       public Dictionary<EffectData,bool> OutputSequene = new Dictionary<EffectData, bool>();

        /// <summary>
        /// 当前显示图像大小
        /// </summary>
        public Size Size { get; set; }

        /// <summary>
        /// 原图大小
        /// </summary>
        public Size OriginalSize { get; set; }

        /// <summary>
        /// 缩小图和原图相差的倍数
        /// </summary>
        public float Multiple { get; set; }

        /// <summary>
        /// 是否有更改
        /// </summary>
        public bool IsChanges = false;


        /// <summary>
        /// 获取输出效果
        /// </summary>
        /// <param name="device">设备用于创建图形资源和绘图会话。 大多数应用 应该使用 CanvasControl 包装器而不是直接管理自己的 CanvasDevice。</param>
        /// <param name="compression">是否压缩图片 true为压缩, false为原图</param>
        /// <returns></returns>
        public abstract ICanvasImage GetEffect(CanvasDevice device,bool compression );

        /// <summary>
        /// 设置，当前效果有更改
        /// </summary>
        protected void SetIsChanges()
        {

            var list = OutputSequene.Keys.ToList();
            foreach (var item in list)
            {
                
               var v= OutputSequene[item];

                OutputSequene[item] = true;
                if (item != this)
                {
                    item.SetIsChanges();
                }
                
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public virtual bool GetIsChanges(EffectData data)
        {
            var change = OutputSequene[data];
            OutputSequene[data] = false;
            return change;
        }

        /// <summary>
        /// 添加输出队列
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public bool AddOutputSequene(EffectData data,bool changes=true)
        {
            if (OutputSequene.ContainsKey(data))
            {
                return false;
            }
            else
            {
                OutputSequene.Add(data, changes);
            }
            return true;
        }

        /// <summary>
        /// 移除指定的输出
        /// </summary>
        /// <param name="data"></param>
        public void RemoveOutputSequene(EffectData data)
        {
            OutputSequene.Remove(data);
        }

        public void Destroy()
        {
            //遍历输出队列

            var list = OutputSequene.Keys.ToList();
            foreach (var item in list)
            {

                if (item is EffectOneSourceData eos)
                {
                    //移除输入
                    eos.RemoveInputEffect(this);
                }

            }

           
         
        }
    }
}
